package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 不出bug- 么么哒
 * @DESCRIPTION
 * @create 2018/11/13
 */
@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;

    @Override
    public void saveOrUpdate(News entity) {
        if (StringUtils.isEmpty (entity.getTitle ()) || (entity.getId () == null && StringUtils.isEmpty (entity.getCoverUrl ()))
                || StringUtils.isEmpty (entity.getNewsContent ().getContent ())) {
            throw new RuntimeException ("不能保存空值");
        }
        if (entity.getId () == null) {
            entity.setNewsTime (new Date ());
            newsMapper.insert (entity);
            NewsContent entityNewsContent = entity.getNewsContent ();
            entityNewsContent.setId (entity.getId ());
            newsContentMapper.insert (entityNewsContent);
        } else {
            entity.setNewsTime (new Date ());
            newsMapper.updateByPrimaryKey (entity);
            NewsContent entityNewsContent = entity.getNewsContent ();
            entityNewsContent.setId (entity.getId ());
            newsContentMapper.updateByPrimaryKey (entityNewsContent);
        }
    }

    @Override
    public void delete(Long id) {
        newsMapper.deleteRelations (id);
        newsMapper.deleteByPrimaryKey (id);
        newsContentMapper.deleteByPrimaryKey (id);
    }

    @Override
    public PageInfo<News> query(NewsQueryObject qo) {
        PageHelper.startPage (qo.getCurrentPage (), qo.getPageSize (), qo.getOrderBy ());
        return new PageInfo<> (newsMapper.selectForList (qo));
    }

    @Override
    public String getContentById(Long id) {
        return newsContentMapper.selectByPrimaryKey (id).getContent ();
    }


    @Override
    public News getNewsById(Long id) {
        return newsMapper.selectByPrimaryKey (id);
    }

    @Override
    public List<News> getRecommendNewesByType(Integer type, Long newsId) {
        int count = newsMapper.countNewesByType (type, newsId);
        if (count >= 5) {
            return newsMapper.selectByType (type, newsId);
        }
        return newsMapper.selectAll (newsId);
    }

    @Override
    public void insertRelation(Long newsesId, Long userId) {
        newsMapper.insertRelations (userId, newsesId);
    }

    @Override
    public void deleteRelation(Long newsId, Long userId) {
        newsMapper.deleteRelation (userId, newsId);
    }

    @Override
    public List<News> selectByUserId(Long userId) {
        return newsMapper.selectByUserId (userId);
    }

}
